var ACTION = require("../base/action");
var utils = require("../utils/utils");

var action = new ACTION("阅读小说");
var defaultActionConfig = {
  per: 0, //每个多少秒执行一次
  records: [],
  startTime: null, //本次开始时间
  endTime: null, //本次结束时间
  execCount: 0, //今日执行次数
  execCost: 0, //本次执行时间,
};
Object.keys(defaultActionConfig).forEach(function (prop) {
  console.log(action[prop]);
});

action.span = 60 * 1000;
action.setSpan = function (span) {
  this.span = span * 1000;
};

function swipeForward() {
  utils.swipeX(
    ((device.width / 10) * 9 * utils.getRandom(90, 100, 1)) / 100,
    ((device.height / 2) * utils.getRandom(90, 100, 1)) / 100,
    ((device.width / 10) * 3 * utils.getRandom(90, 100, 1)) / 100,
    ((device.height / 3) * utils.getRandom(90, 100, 1)) / 100,
    utils.getRandom(0.5, 1.2, 1000)
  );
}
function swipeBack() {
  utils.swipeX(
    ((device.width / 10) * 3 * utils.getRandom(90, 100, 1)) / 100,
    ((device.height / 2) * utils.getRandom(90, 100, 1)) / 100,
    ((device.width / 10) * 9 * utils.getRandom(90, 100, 1)) / 100,
    ((device.height / 3) * utils.getRandom(90, 100, 1)) / 100,
    utils.getRandom(0.5, 1.2, 1000)
  );
}

action.exec = function () {
  var startTime = Date.now();
  var span = this.span || 60 * 1000; //60s.
  console.log(this.name, "将运行-" + span / 1000 + "-s");
  var swipeIndex = 0;
  while (true) {
    sleep(utils.getRandom(3.5, 5.5, 1000));
    var randomNumber = utils.getRandom(1, 100);
    if (swipeIndex < 10) {
      swipeForward();
    } else {
      if (randomNumber < 90) {
        swipeForward();
      } else {
        swipeBack();
      }
    }
    if (randomNumber > 90) {
      sleep(utils.getRandom(10, 12, 1000));
    }
    swipeIndex = swipeIndex + 1;
    var endTime = Date.now();
    if (endTime - startTime > span) {
      break;
    }
  }
};

module.exports = action;
